package com.zzw.controller;

import com.zzw.entity.PageResult;
import com.zzw.entity.Result;
import com.zzw.message.MessageConstant;
import com.zzw.pojo.OilWorker;
import com.github.pagehelper.PageInfo;
import com.zzw.service.OilWorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/oilWorker")
public class OilWorkerController {
    @Autowired
    private OilWorkerService oilWorkerService;
    @GetMapping("/{pageNum}/{pageSize}")
    public PageResult findPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageInfo pageInfo = oilWorkerService.findPage(pageNum, pageSize);
        long total = pageInfo.getTotal();
        List rows = pageInfo.getList();
        return new PageResult(total,rows);
    }

    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<OilWorker> list = oilWorkerService.findAll();
            return new Result(true,MessageConstant.QUERY_OILWORKER_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_OILWORKER_FAIL);
        }
    }


    @PostMapping("/add")
    public Result addOilWorker(@RequestBody OilWorker oilWorker){
        try {
            oilWorkerService.addOilWorker(oilWorker);
            return new Result(true, MessageConstant.ADD_OILWORKER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_OILWORKER_FAIL);
    }
}
